package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.domain.Menu;
import com.li.domain.RoleMenu;
import com.li.exception.NotFondException;
import com.li.mapper.MenuMapper;
import com.li.mapper.RoleMenuMapper;
import com.li.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public String addMenu(Menu menu) {
        if (menu==null){
            throw new NotFondException("菜单为空");
        }
        menu.setCreateTime(new Date());
        int insert = menuMapper.insert(menu);
        return String.valueOf(insert);
    }

    @Override
    public String updateMenu(Menu menu) {
        if (menu==null){
            throw new NotFondException("菜单为空");
        }
        int insert = menuMapper.updateById(menu);
        return String.valueOf(insert);
    }

    @Override
    public List<Menu> listByIdsAndOrderNum(List<String> asList) {
        return menuMapper.listByIdsAndOrderNum(asList);
    }

    @Override
    public List<Menu> listQueryMenuTree(Menu menu) {
        return menuMapper.listQueryMenuTree(menu);
    }

    @Transactional
    @Override
    public boolean delMenu(Integer menuId) {
        try{
            roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("menu_id",menuId));
            menuMapper.deleteById(menuId);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
